/**
 * JAVACC DEMO 1.0
 */
package com.apache.uct.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.tools.ClassToolsUtil;
import com.apache.uct.common.entity.Dept;
import com.apache.uct.common.entity.Org;
import com.apache.uct.manager.DeptManager;
import com.apache.uct.manager.OrgManager;
import java.util.Date;

/**
 * description:  机构信息保存
 *
 * @author Hou Dayu 创建时间：2016-8-5
 */
public class OrgSavePlugin implements PluginConnector {

    private OrgManager orgManager;

    private DeptManager deptManager;

    /**
     * TODO 机构信息保存.
     *
     * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)
     */
    public Object execute(ParamsVo vo) throws Exception {
        String orgEname = String.valueOf(vo.getParams("orgEname"));
        String orgCname = String.valueOf(vo.getParams("orgCname"));

        ResultEntity entity = new ResultEntity();
        if (Validator.isNull(orgEname) || Validator.isNull(orgCname)) {
            entity.setMessage("机构代码与机构名称不可为空");
            entity.setEntity("");
        } else {
            Org org = new Org();
            org = (Org) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), org);
            ParamsVo<Org> pvo = new ParamsVo<Org>();
            pvo.setObj(org);

            String orgId = String.valueOf(vo.getParams("orgId"));
            if (Validator.isNotNull(orgId)) {
                orgManager.editInfo(pvo);
            } else {
                orgId = orgManager.saveInfo(pvo);
                String ifSaveDept = String.valueOf(vo.getParams("ifSaveDept"));
                if ("T".equalsIgnoreCase(ifSaveDept) && Validator.isNotNull(orgId)) {
                    Dept dept = new Dept();
                    dept.setDeptId(orgId);
                    dept.setDeptEname(org.getOrgEname() + "_member");
                    dept.setDeptCname("会员部");
                    dept.setDeptStatus(1);
                    dept.setOrgId(orgId);
                    dept.setDeptRemark("");
                    dept.setFullEname("");
                    dept.setFullCname("");
                    dept.setFatherId("0");
                    dept.setOrgEname(org.getOrgEname());
                    ParamsVo dvo = new ParamsVo();
                    dvo.setObj(dept);
                    deptManager.saveInfo(dvo);

                    dept = new Dept();
                    dept.setDeptId("dept"+String.valueOf(new Date().getTime()));
                    dept.setDeptEname(org.getOrgEname() + "_sys");
                    dept.setDeptCname("技术部");
                    dept.setDeptStatus(1);
                    dept.setOrgId(orgId);
                    dept.setDeptRemark("");
                    dept.setFullEname("");
                    dept.setFullCname("");
                    dept.setFatherId("0");
                    dept.setOrgEname(org.getOrgEname());
                    dvo = new ParamsVo();
                    dvo.setObj(dept);
                    deptManager.saveInfo(dvo);

                }
            }
            entity.setEntity("");
            if (Validator.isNotNull(orgId)) {
                entity.setMessage("机构" + org.getOrgCname() + "添加成功");
                entity.setEntity(orgId);
            } else {
                entity.setMessage("机构" + org.getOrgCname() + "添加失败");
            }
        }
        entity.setResult("true");
        return entity;
    }

    //组装用户信息
    private Org buildOrgInfo(ParamsVo vo, Org org) {
        org.setFatherId(Validator.getDefaultStr(String.valueOf(vo.getParams("fatherId")), "0"));
        org.setOrgCode(getParamStr(vo, "orgCode"));
        org.setAddress(getParamStr(vo, "address"));
        org.setBoss(getParamStr(vo, "boss"));
        org.setCity(getParamStr(vo, "city"));
        org.setCreateUser(getParamStr(vo, "createUser"));
        org.setEmail(getParamStr(vo, "email"));
        org.setFax(getParamStr(vo, "fax"));
        org.setHomepage(getParamStr(vo, "homepage"));
        org.setLinkMan(getParamStr(vo, "linkMan"));
        org.setMobile(getParamStr(vo, "linkMan"));
        org.setOrgRegMoney(getParamStr(vo, "orgRegMoney"));
        org.setOrgRemark(getParamStr(vo, "orgRemark"));
        org.setOrgScope(getParamStr(vo, "orgScope"));
        org.setOrgStatus(1);
        org.setOrgType(getParamStr(vo, "orgType"));
        org.setPhone(getParamStr(vo, "phone"));
        org.setProvince(getParamStr(vo, "province"));
        org.setPostCode(getParamStr(vo, "postCode"));
        org.setCreateTime(System.currentTimeMillis());
        org.setRegTime(org.getCreateTime());
        org.setSysEname(getParamStr(vo, "sysEname"));
        org.setOrgId(getParamStr(vo, "orgId"));
        org.setUpdateCount(0);
        org.setSubCount(0);
        org.setFullEname("/" + org.getOrgEname());
        org.setFullCname("/" + org.getOrgCname());
        if (!"0".equals(org.getFatherId())) {
            ParamsVo<Org> pvo = new ParamsVo<Org>();
            pvo.setInfoId(org.getFatherId());
            Org porg = (Org) orgManager.getInfoById(pvo);
            if (!Validator.isEmpty(porg)) {
                org.setFullEname(porg.getFullEname() + "/" + org.getOrgEname());
                org.setFullCname(porg.getFullCname() + "/" + org.getOrgCname());
            }
        }
        return org;
    }

    private String getParamStr(ParamsVo vo, String attrName) {
        return Validator.getDefaultStr(String.valueOf(vo.getParams(attrName)), "");
    }

    public void setOrgManager(OrgManager orgManager) {
        this.orgManager = orgManager;
    }

    public void setDeptManager(DeptManager deptManager) {
        this.deptManager = deptManager;
    }

}
